<?php

namespace app\admin\controller;
use org\Tree;
class Carousel extends base
{

    /**
     * @var 前置方法
     */
    protected $beforeActionList = [
        'delfile' => ['only' => 'del'],
        'addclass' => ['only' => 'add,edit'],
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
            if (!empty($data['model'])) {
                $this->datas ['model'] = $data['model'];
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }

    /**
     * 删除文件
     */
    protected function delfile()
    {
        $file = $this->model->where('id', $this->request->param('id'))->value('image');
        if (!empty($file) && file_exists(__IMAGE__. $file)) {
            @unlink(__IMAGE__. $file);
        }
    }

    /**
     * 栏目
     */
    protected function addclass()
    {
//        $nav = model('nav')->catetree();
//        $this->assign('nav', $nav);
        $data=[
            'lang'=>cookie('think_var_admin')
        ];
        $rule = model('nav')->where($data)->select();
        $auth_rule = Tree::Forlayer($rule,'child','id',  'pid');
        $this->assign('auth_rule', $auth_rule);

    }




//    public function index()
//    {
//        $list = $this->model
//            ->order('sort desc')
//            ->paginate();
//
//        foreach ($list as $key => $value) {
//            $nav = $list[$key]['van_name'] = '';
//            $type = explode(',', $list[$key]->type);
//            foreach ($type as $vkey) {
//                $nav .= $vkey==='0'?'首页-':'';
//                if ($vkey) {
//                    $nav .= model('Nav')->where('id', $vkey)->value('name') . '-';
//                }
//                $list[$key]['van_name'] = $nav;
//            }
//        }
//        $this->assign('num', $this->model->count());
//        $this->assign('list', $list);
//        return $this->fetch();
//    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        $data = $this->request->post();
        $result = $this->validate($data, 'Carousel');
        if (true !== $result) {
            // 验证失败 输出错误信息
            return ajax_info($result);
        }

        $data['type'] = !empty($data['nav_id']) ? ',' . implode(',', $data['nav_id']) . ',' : '';


        if (!empty($data['id'])) {
            /**
             * 删除旧文件
             */
            if ($data['image'] != $data['old_image']) {
                @unlink(__IMAGE__. $data['old_image']);
            }
            deldir(RUNTIME_PATH);
            return $this->update($data);
        }
        $data['lang']=cookie('think_var_admin');

        $res = $this->model->allowField(true)->save($data);
        if ($res) {
            return ajax_info('保存成功','y');
        } else {
            return ajax_info('添加失败');
        }
    }
}
